package com.example.demo.packageTest;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.util.Date;

public class PackageClient {
    private  final StringDecoder DECODER = new StringDecoder();
    private  final StringEncoder ENCODER = new StringEncoder();

    public void connect(int port,String host) throws Exception{
        EventLoopGroup group = new NioEventLoopGroup();
        try{
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group).channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY,true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel channel) throws Exception {
                            channel.pipeline().addLast(new MyMessageDecoder());
                            channel.pipeline().addLast(new MyMessageEncoder());
                            channel.pipeline().addLast(new PackageClientHandler());
                        }
                    });
            ChannelFuture future = bootstrap.connect(host,port).sync();
            //future.channel().writeAndFlush("query time order");
            future.channel().closeFuture().sync();
        }finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception{
       new PackageClient().connect(9000,"localhost");

    }
}
